package pl.jtb.ctl.tabory.ui.boe.ui.te;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.layout.GridLayoutFactory;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.ui.forms.SectionPart;
import org.eclipse.ui.forms.widgets.FormToolkit;

import pl.jtb.ctl.tabory.dao.data.vo.StationVO;
import pl.jtb.ctl.tabory.dao.data.vo.TripVO;
import pl.jtb.ctl.tabory.ui.boe.osgi.Activator;
import pl.jtb.ctl.tabory.ui.boe.selection.TripSelection;
import pl.jtb.ctl.tabory.ui.selection.ISelectionListener;
import pl.jtb.ctl.tabory.ui.selection.ISelectionProvider;

public class TripSection extends SectionPart implements ISelectionProvider<TripSelection> {

    private TripSelection selection;
    private String title;

    private TableViewer tableViewer;
    private Table table;

    private List<ISelectionListener<TripSelection>> listSelectionListeners = new ArrayList<ISelectionListener<TripSelection>>();


    public TripSection(Composite parent, FormToolkit toolkit, int style, String title) {
        super(parent, toolkit, style);
        this.title = title;
        initGui(toolkit);
        postInitGui();
    }

    private void initGui(FormToolkit toolkit) {
        getSection().setText(title);
        Composite container = toolkit.createComposite(getSection());
        GridLayoutFactory.fillDefaults().numColumns(1).applyTo(container);
        GridDataFactory.fillDefaults().align(SWT.FILL, SWT.FILL).grab(true, true).applyTo(container);
        getSection().setClient(container);

        tableViewer = new TableViewer(container, SWT.BORDER | SWT.FULL_SELECTION);
        table = tableViewer.getTable();
        table.setHeaderVisible(true);
        toolkit.paintBordersFor(table);
        GridDataFactory.fillDefaults().align(SWT.FILL, SWT.FILL).grab(true, true).applyTo(table);

        TableViewerColumn viewerColumnStationFrom = new TableViewerColumn(tableViewer, SWT.NONE);
        TableViewerColumn viewerColumnStationTo = new TableViewerColumn(tableViewer, SWT.NONE);
        TableViewerColumn viewerColumnDateFrom = new TableViewerColumn(tableViewer, SWT.NONE);
        TableViewerColumn viewerColumnDateUntil = new TableViewerColumn(tableViewer, SWT.NONE);
        TableViewerColumn viewerColumnDistance = new TableViewerColumn(tableViewer, SWT.NONE);

        TableColumn columnStationFrom = viewerColumnStationFrom.getColumn();
        TableColumn columnStationTo = viewerColumnStationTo.getColumn();
        TableColumn columnDateFrom = viewerColumnDateFrom.getColumn();
        TableColumn columnDateUntil = viewerColumnDateUntil.getColumn();
        TableColumn columnDistance = viewerColumnDistance.getColumn();

        columnDateFrom.setText(Activator.getMessage("trip.editor.tiptable.col.datefrom"));
        columnDateFrom.setWidth(140);

        columnDateUntil.setText(Activator.getMessage("trip.editor.tiptable.col.dateuntil"));
        columnDateUntil.setWidth(140);

        columnDistance.setText(Activator.getMessage("trip.editor.tiptable.col.distance"));
        columnDistance.setWidth(100);

        columnStationFrom.setText(Activator.getMessage("trip.editor.tiptable.col.stationfrom"));
        columnStationFrom.setWidth(140);

        columnStationTo.setText(Activator.getMessage("trip.editor.tiptable.col.stationto"));
        columnStationTo.setWidth(140);

        tableViewer.setLabelProvider(new TableLabelProvider());
        tableViewer.setContentProvider(new TableContentProvider());
    }

    private void postInitGui() {
        tableViewer.addSelectionChangedListener(new ISelectionChangedListener() {

            @Override
            public void selectionChanged(SelectionChangedEvent event) {
                IStructuredSelection tableSelection = (IStructuredSelection) event.getSelection();
                TripVO selectedElement = (TripVO) tableSelection.getFirstElement();
                if (selectedElement != null) {
                    selection.setSelectedTrip(selectedElement);
                }
                for (ISelectionListener<TripSelection> listener : listSelectionListeners) {
                    listener.selectionChanged(selection);
                }
            }
        });
    }

    @Override
    public TripSelection getSelection() {
        return selection;
    }

    @Override
    public void setSelection(TripSelection selection) {
        this.selection = selection;
        tableViewer.setInput(selection.getListTrips());
    }

    @Override
    public void addSelectionListener(ISelectionListener<TripSelection> listner) {
        listSelectionListeners.add(listner);

    }

    @Override
    public void removeSelectionListener(ISelectionListener<TripSelection> listner) {
        listSelectionListeners.remove(listner);
    }

    private class TableLabelProvider extends LabelProvider implements ITableLabelProvider {

        DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");

        public TableLabelProvider() {
            super();
        }

        @Override
        public Image getColumnImage(Object element, int columnIndex) {
            return null;
        }

        @Override
        public String getColumnText(Object element, int columnIndex) {
            TripVO trip = (TripVO) element;
            switch (columnIndex) {
                case 0: {
                    StationVO station = trip.getStationFrom();
                    if (station != null) {
                        return station.getStationName();
                    } else {
                        return "-";
                    }
                }
                case 1: {
                    StationVO station = trip.getStationTo();
                    if (station != null) {
                        return station.getStationName();
                    } else {
                        return "-";
                    }
                }
                case 2: {
                    Date date = trip.getDateFrom();
                    return dateFormat.format(date);
                }
                case 3: {
                    Date date = trip.getDateUntil();
                    return dateFormat.format(date);
                }
                case 4: {
                    double distanceKm = trip.getDistanceKm();
                    return String.valueOf(distanceKm);
                }
                default:
                    return null;
            }
        }

    }

    private static class TableContentProvider implements IStructuredContentProvider {

        @Override
        public void dispose() {
        }

        @Override
        public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
        }

        @Override
        @SuppressWarnings("unchecked")
        public Object[] getElements(Object inputElement) {
            List<TripVO> listVagons = (List<TripVO>) inputElement;
            return listVagons.toArray();
        }

    }

}
